package org.example.ims.Controller.Order;

import org.example.ims.DTO.Response;
import org.example.ims.Entity.Order.TransferOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.example.ims.Service.Order.TransferOrderService;


@RestController
@RequestMapping("/transferOrder")
public class TransferOrderController {

    @Autowired
    private TransferOrderService transferOrderService;

    /**
     * 创建转仓单
     * @param order 转仓单对象
     * @return 创建的转仓单
     */
    @PostMapping
    public ResponseEntity<TransferOrder> createOrder(@RequestBody TransferOrder order) {
        TransferOrder createdOrder = transferOrderService.createOrder(order);
        return ResponseEntity.ok(createdOrder);
    }

    /**
     * 执行转仓单
     * @param orderId 转仓单ID
     * @return 无内容响应
     */
    @PostMapping("/{orderId}/execute")
    public ResponseEntity<Void> executeOrder(@PathVariable Integer orderId) {
        transferOrderService.executeOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 更新转仓单
     * @param orderId 转仓单ID
     * @param order 更新后的转仓单对象
     * @return 更新后的转仓单
     */
    @PutMapping("/{orderId}")
    public ResponseEntity<TransferOrder> updateOrder(@PathVariable Integer orderId, @RequestBody TransferOrder order) {
        TransferOrder updatedOrder = transferOrderService.updateOrder(orderId, order);
        return ResponseEntity.ok(updatedOrder);
    }

    /**
     * 删除转仓单
     * @param orderId 转仓单ID
     * @return 无内容响应
     */
    @DeleteMapping("/{orderId}")
    public ResponseEntity<Void> deleteOrder(@PathVariable Integer orderId) {
        transferOrderService.deleteOrder(orderId);
        return ResponseEntity.noContent().build();
    }

    /**
     * 根据ID获取转仓单
     * @param orderId 转仓单ID
     * @return 转仓单对象
     */
    @GetMapping("/{orderId}")
    public ResponseEntity<TransferOrder> getOrderById(@PathVariable Integer orderId) {
        TransferOrder order = transferOrderService.getOrderById(orderId);
        return ResponseEntity.ok(order);
    }

    /**
     * 获取所有转仓单（分页）
     * @param page 页码
     * @param size 每页大小
     * @return 转仓单分页结果
     */
    @GetMapping("/search")
    public ResponseEntity<Page<TransferOrder>> getAllOrders(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<TransferOrder> orders = transferOrderService.getAllOrders(page, size);
        return ResponseEntity.ok(orders);
    }

    /**
     * 搜索转仓单
     * @param status 状态（可选）
     * @param sourceWarehouseId 源仓库ID（可选）
     * @param targetWarehouseId 目标仓库ID（可选）
     * @param salesmanId 业务员ID（可选）
     * @param page 页码
     * @param size 每页大小
     * @return 搜索结果包装在Response对象中
     */
    @GetMapping
    public ResponseEntity<Response<Page<TransferOrder>>> searchOrders(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) Integer sourceWarehouseId,
            @RequestParam(required = false) Integer targetWarehouseId,
            @RequestParam(required = false) Integer salesmanId,
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        Page<TransferOrder> orders = transferOrderService.searchOrders(
                status, sourceWarehouseId, targetWarehouseId, salesmanId, page, size);
        return ResponseEntity.ok(Response.success(orders));
    }
}
